//
//  CommentViewController.m
//  Cup
//
//  Created by dllo on 15/4/23.
//  Copyright (c) 2015年 doll. All rights reserved.
//

#import "CommentViewController.h"
#import "dataBaseHandle.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface CommentViewController ()
@property(nonatomic, retain)NSMutableArray *contentArr;
@property(nonatomic, retain)UITableView *tableview;
@property(nonatomic, assign)NSInteger page;
   @end

@implementation CommentViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.contentArr = [NSMutableArray array];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatview];
    [self postData];
    [self method];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(turnBack)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:59/255.0 green:54/255.0 blue:24/255.0 alpha:1];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"Top" forState:UIControlStateNormal];
    button.frame = CGRectMake(0.8 * WIDTH, 0.7 * HEIGHT - 10, 36, 36);
    button.layer.borderWidth = 1;
    button.backgroundColor = [UIColor blackColor];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.clipsToBounds = YES;
    button.alpha = 0.8;
    button.layer.cornerRadius = 18;
    [button addTarget:self action:@selector(moveToTop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)moveToTop
{
    self.tableview.contentOffset = CGPointMake(0, 0);
}

- (void)turnBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

// 上拉加载
- (void)method
{
    [self.tableview addLegendFooterWithRefreshingBlock:^{
        [self postData];
    }];
}

- (void)postData
{
    if ([dataBaseHandle shareDB].state == 1) {
        NSString *i = [NSString stringWithFormat:@"%ld",self.page*4];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"D8FDE94C-841F-4A4D-B5C4-AAD2472CEE85",@"deviceid",@"1",@"client",self.idstr,@"contentid",@"10",@"limit",@"3.0.1",@"version",i,@"start",@"",@"auth", nil];
        [manager POST:@"http://api2.pianke.me/comment/get" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSMutableDictionary *dic = responseObject;
            NSMutableDictionary *dataDic = dic[@"data"];
            NSMutableArray *listArr = dataDic[@"list"];
            
            for (NSMutableDictionary *temp in listArr) {
                NSMutableDictionary *usedic = temp[@"userinfo"];
                Read *readMo = [[Read alloc] init];
                [readMo setValuesForKeysWithDictionary:usedic];
                [readMo setValuesForKeysWithDictionary:temp];
                [self.contentArr addObject:readMo];
            }
            [self.tableview reloadData];
            [self.tableview.footer endRefreshing];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        }];
        self.page++;
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前无网络，请检查网络后重新尝试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
}

- (void)creatview
{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    [_tableview release];
    self.tableview.delegate =self;
    self.tableview.dataSource =self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contentArr.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    ContentCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[ContentCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }
    //设置点击cell不变色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSURL *url = [NSURL URLWithString:[self.contentArr[indexPath.row] icon]];
    [cell.perImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"touxiangzhanweitu"]];
    cell.nameLabel.text = [self.contentArr[indexPath.row]uname];
    cell.contLabel.text = [self.contentArr[indexPath.row] content];
    cell.timeLabel.text = [self.contentArr[indexPath.row] addtime_f];
    return cell;
}

//自定义文字高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //根据对应的文字求出label显示需要的高度
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName, nil];
    //根据文本大小(font)计算出文本的rect值,最大范围不超过(375,400);
    CGRect rect = [[self.contentArr[indexPath.row]content]boundingRectWithSize:CGSizeMake(375, 400) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height + 100;
}

- (void)dealloc
{
    [self.contentArr release];
    [_tableview release];
    [super dealloc];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
